<!--
 * @Description:
 * @Author: 稽辉
 * @Date: 2025-01-18 10:15:10
 * @LastEditTime: 2025-03-08 08:41:44
 * @LastEditors: 稽辉
-->
<script setup lang="ts">
//
import { onLoad } from '@dcloudio/uni-app'
import CustomNavBar from './components/CustomNavbar.vue'
import { ref } from 'vue'
import CategoryPanel from './components/CategoryPanel.vue'
import type { DCRecommendInstance } from '@/types/component'
import PageSkeleton from './components/PageSkeleton.vue'
const bannerList = ref<Banners[]>([])
import { type Banners, type Categories } from '@/api/index'
import { getHomeBannerAPI, getHomeCategoryAPI } from '@/services/home'
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI(1)
  bannerList.value = res.data
}
const categoryList = ref<Categories[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.data
}
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData()])
  isLoading.value = false
})
//获取菜品推荐的组件实例
const guessRef = ref<DCRecommendInstance>()
const onScrollToLower = () => {
  console.log('滚动触底')
  guessRef.value?.getMore()
}
const isTrigger = ref(false)
const isLoading = ref(true)
const onRefresherRefresh = async () => {
  isTrigger.value = true
  isLoading.value = true
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), guessRef.value?.getMore()])
  isTrigger.value = false
  isLoading.value = false
}
</script>

<template>
  <CustomNavBar />
  <!-- 滚动容器，绑定事件 -->
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrollToLower"
    :refresher-enabled="true"
    @refresherrefresh="onRefresherRefresh"
    :refresher-triggered="isTrigger"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <DCSwiper :list="bannerList" />
      <CategoryPanel :list="categoryList" />
      <DCRecommend ref="guessRef" title="菜品推荐" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
//
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
